package com.unbox.admin.controller;

import com.alibaba.fastjson.JSON;
import com.unbox.admin.entity.Enumeration;
import com.unbox.admin.fegin.EnumerationFegin;
import com.unbox.admin.service.IEnumerationService;
import com.unbox.common.configuration.redis.UnboxCache;
import com.unbox.common.response.UnboxResponse;
import com.unbox.common.table.UnBoxPage;
import com.unbox.common.util.UnBoxUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * <p>
 * <枚举管理业务Controller
 * </p>
 *
 * @author 王振宇
 * @since 2021-03-02
 */
@Api(tags = "枚举管理管理api")
@RestController
@Log4j2
public class EnumerationController implements EnumerationFegin {

    @Autowired
    IEnumerationService enumerationService;

    /**
     * 获取缓存工具包
     */
    UnboxCache cache = new UnBoxUtils.Cache();

    /**
     * 分页查询
     * @param enumeration
     * @return
     */
    @ApiOperation(value = "分页查询列表")
    @Override
    public UnboxResponse<UnBoxPage> getPageList(Enumeration enumeration, Integer pageNo, Integer pageSize) {
        UnBoxPage page = enumerationService.getPageList(enumeration,pageNo, pageSize);
        return new UnboxResponse<>(page);
    }

    @Override
    public UnboxResponse<List<Enumeration>> getList(Enumeration enumeration) {
        Object cacheValue = cache.get("Enumeration_" + enumeration.getName());
        if(cacheValue != null) {
            List<Enumeration> jsonArrayData = JSON.parseArray(cacheValue.toString(), Enumeration.class);
            return new UnboxResponse<>(jsonArrayData);
        }
        List<Enumeration> enumerations = enumerationService.getList(enumeration);
        cache.set("Enumeration_"+enumeration.getName(), JSON.toJSONString(enumerations), UnboxCache.DEFAULT_DELETE_TIME);
        return new UnboxResponse<>(enumerations);
    }

    /**
     * 详情
     * @param enumeration
     * @return
     */
    @ApiOperation(value = "查询明细")
    @Override
    public UnboxResponse<Enumeration> info(Enumeration enumeration) {
        Enumeration enumerationVo = enumerationService.get(enumeration);
        return new UnboxResponse<>(enumerationVo);
    }

    /**
     * 通过id数据查询数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "通过ids进行in查询")
    @Override
    public UnboxResponse<List<Enumeration>> idsEnumerations(List<Integer> ids) {
        List<Enumeration> enumerations = enumerationService.getIdsList(ids);
        return new UnboxResponse<>(enumerations);
    }

    /**
     * 保存数据
     * @param enumeration
     * @return
     */
    @ApiOperation(value = "保存数据")
    @Override
    public UnboxResponse<Boolean> save(Enumeration enumeration) {
        Boolean flg = enumerationService.save(enumeration);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量添加
     * @param enumerations
     * @return
     */
    @ApiOperation(value = "批量保存数据")
    @Override
    public UnboxResponse<Boolean> batchSave(List<Enumeration> enumerations) {
        Boolean flg = enumerationService.saveBatch(enumerations);
        return new UnboxResponse<>(flg);
    }

    /**
     * 修改数据
     * @param enumeration
     * @return
     */
    @ApiOperation(value = "修改数据")
    @Override
    public UnboxResponse<Boolean> update(Enumeration enumeration) {
        Boolean flg = enumerationService.update(enumeration);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量修改
     * @param enumerations
     * @return
     */
    @ApiOperation(value = "批量修改数据")
    @Override
    public UnboxResponse<Boolean> batchUpdate(List<Enumeration> enumerations) {
        Boolean flg = enumerationService.updateBatchById(enumerations);
        return new UnboxResponse<>(flg);
    }

    /**
     * 删除数据
     * @param enumeration
     * @return
     */
    @ApiOperation(value = "删除数据")
    @Override
    public UnboxResponse<Boolean> delete(Enumeration enumeration) {
        Boolean flg = enumerationService.remove(enumeration);
        return new UnboxResponse<>(flg);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @Override
    public UnboxResponse<Boolean> deleteBatch(List<Integer> ids) {
        Boolean flg = enumerationService.removeByIds(ids);
        return new UnboxResponse<>(flg);
    }
}

